﻿/**
* 该类为皮肤加载管理类
* @author lbynet
* @version 0.1
*/

package {

	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;

	public class FlexSkinManager {
		public static  const SKINCOMPLETE:String="skincomplete";
		public var skinPath:String;
		public var loader:Loader;
		
		private var backUp:Function;
		public function FlexSkinManager() {
			if (instance != null) {
				throw new Error("不能直接创建对象");
			}
			loader=new Loader();
		}
		private static  var instance:FlexSkinManager=new FlexSkinManager();

		public static function getInstance():FlexSkinManager {
			return instance;
		}
		public function get Path():String {
			return this.skinPath;
		}
		public function loadSkin(_path:String, _backUp:Function = null):void {
			
			this.skinPath=_path;
			if (_backUp != null) {
				backUp = _backUp;
			}
			loader.load(new URLRequest(this.skinPath));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
		}
		private function completeHandler(event:Event) {
			trace("皮肤加载完成!");
			//移除侦听器
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
			//当loader成功，而不将loader进来的对象加入显示列表的时候就会报错
			if (this.backUp != null) this.backUp(event);
		}
		public function getClass(className:String):Class {
			try {
				return loader.contentLoaderInfo.applicationDomain.getDefinition(className)  as  Class;
			} catch (e:Error) {
				throw new Error(className + " definition not found in " + e.toString());
			}
			return null;
		}
	}
}